
package org.icnmasjid.member.dao.hibernate;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.icnmasjid.member.dao.RegistrationDao;
import org.icnmasjid.member.model.AfaReg;
import org.icnmasjid.member.model.Registration;
import org.icnmasjid.member.model.SchoolRegistration;
import org.springframework.orm.ObjectRetrievalFailureException;

public class RegistrationDaoHibernate extends BaseDaoHibernate implements RegistrationDao {

    /**
     * @see org.icnmasjid.member.dao.RegistrationDao#getRegistrations(org.icnmasjid.member.model.Registration)
     */
    public List getRegistrations(final Registration registration) {
    	if ( registration != null ) {
    		Criteria c = getSession().createCriteria(Registration.class);
    		if ( registration.getTicketNumbers() != null ) {
    			c.add(
					Restrictions.like("ticketNumbers", registration.getTicketNumbers(), MatchMode.ANYWHERE)
    			);
    		}

    		if ( StringUtils.isNotBlank(registration.getFirstName()) ) {
    			c.add(Restrictions.ilike("firstName", registration.getFirstName().trim(), MatchMode.START));
    		}
    		if ( StringUtils.isNotBlank(registration.getLastName()) ) {
    			c.add(Restrictions.ilike("lastName", registration.getLastName().trim(), MatchMode.START));
    		}
    		if ( StringUtils.isNotBlank(registration.getEmail()) ) {
    			c.add(
					Restrictions.ilike("email", registration.getEmail().trim(), MatchMode.START)
    			);
    		}
    		if ( StringUtils.isNotBlank(registration.getPhone()) ) {
    			c.add(
					Restrictions.like("phone", registration.getPhone().trim(), MatchMode.START)
    			);
    		}
    		if ( registration.getOnlinePurchase() != null && registration.getOnlinePurchase().booleanValue() ) {
    			c.add(
    				Restrictions.eq("onlinePurchase", registration.getOnlinePurchase())
        		);
    		}
    		return c.list();
    	} else {
    		return getHibernateTemplate().find("from Registration");
    	}
    }

    /**
     * @see org.icnmasjid.member.dao.RegistrationDao#getRegistration(Integer registrationId)
     */
    public Registration getRegistration(final Integer registrationId) {
        Registration registration = (Registration) getHibernateTemplate().get(Registration.class, registrationId);
        if (registration == null) {
            log.warn("uh oh, registration with registrationId '" + registrationId + "' not found...");
            throw new ObjectRetrievalFailureException(Registration.class, registrationId);
        }

        return registration;
    }

    /**
     * @see org.icnmasjid.member.dao.RegistrationDao#saveRegistration(Registration registration)
     */
    public void saveRegistration(final Registration registration) {
        getHibernateTemplate().saveOrUpdate(registration);
    }

    /**
     * @see org.icnmasjid.member.dao.RegistrationDao#removeRegistration(Integer registrationId)
     */
    public void removeRegistration(final Integer registrationId) {
        getHibernateTemplate().delete(getRegistration(registrationId));
    }


    public Map<String, Integer> getFamilyCheckedInCount() {
    	StringBuffer sb = new StringBuffer("select new Map(count(reg) as familyCount, sum(reg.numberTickets) as individualCount) from Registration reg where reg.checkedIn = 1");
    	return (Map<String, Integer>)getSession().createQuery(sb.toString()).list().get(0);
    }

    public Map<String, Integer> getAmtCollected() {
    	StringBuffer sb = new StringBuffer("select new Map(sum(reg.amtPaid) as amtPaid, sum(reg.amtPaidAtRegistration) as amtPaidAtRegistration) from Registration reg");
    	return (Map<String, Integer>)getSession().createQuery(sb.toString()).list().get(0);
    }

    public SchoolRegistration getExistingSchoolRegistration(SchoolRegistration reg) {
    	List l = getHibernateTemplate().findByExample(reg);
    	return (SchoolRegistration)(l.size() > 0 ? l.get(0) : null);
    }
    
    public SchoolRegistration merge(SchoolRegistration sr) {
    	return (SchoolRegistration)getHibernateTemplate().merge(sr);
    }
    
    public List<SchoolRegistration> getExistingSchoolRegistrations(SchoolRegistration reg) {
    	List<SchoolRegistration> l = getHibernateTemplate().findByExample(reg);
    	return l;
    }
    
    public List<AfaReg> getAfaRegistrations(AfaReg afaReg) {
    	List<AfaReg> l = getHibernateTemplate().findByExample(afaReg);
    	return l;
    }
    
}
